﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Document
{
    /// <summary>
    /// 标识一个交互标记
    /// </summary>
    public sealed class AcorisMarkdownInteraction:IEqualityComparer<AcorisMarkdownInteraction>
    {
        string _id;

        #region Constructor

        public AcorisMarkdownInteraction()
        {
            _id = RandomResourceIdGenerator.Gen();
        }

        public AcorisMarkdownInteraction(string id)
        {
            _id = id ?? throw new NullReferenceException();
        }

        #endregion

        /// <summary>
        /// 解决冲突
        /// </summary>
        internal void Gen()
        {
            _id = RandomResourceIdGenerator.Gen();
        }

        #region Property

        /// <summary>
        /// 标识交互类型
        /// </summary>
        public AcorisMarkdownInteractionType Type { get; set; }

        /// <summary>
        /// 标识交互标记所关联的资源
        /// </summary>
        public AcorisMarkdownResource Source { get; set; }

        /// <summary>
        /// 标识当前交互标记的ID
        /// </summary>
        public string ID {
            get {
                return _id;
            }
        }

        #endregion

        #region Override

        public override bool Equals(object obj)
        {
            if(obj is AcorisMarkdownInteraction ami) {
                return ami._id == this._id;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() ^ _id.GetHashCode();
        }

        public override string ToString()
        {
            return $"{{{Type},{_id}}}";
        }

        #endregion

        #region IEqualityComparer<AcorisMarkdownInteraction> 接口成员

        public bool Equals(AcorisMarkdownInteraction x, AcorisMarkdownInteraction y)
        {
            return x._id == y._id;
        }

        public int GetHashCode(AcorisMarkdownInteraction obj)
        {
            return obj.GetHashCode();
        }

        #endregion
    }
}
